package org.yuri.phoneassistant.m3u8dvideo.controller;

import com.yuri.platform.utils.ajax.AjaxJson;
import com.yuri.platform.utils.file.FileTypeCheck;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.yuri.phoneassistant.m3u8dvideo.entity.M3u8Video;
import org.yuri.phoneassistant.m3u8dvideo.service.M3u8ServerService;

import java.io.File;
import java.util.List;

@RestController
@RequestMapping("/m3u8video")
public class M3u8ServerController {

    M3u8ServerService m3u8ServerService;

    @Autowired
    public M3u8ServerController(M3u8ServerService m3u8ServerService) {
        this.m3u8ServerService = m3u8ServerService;
    }

    @GetMapping("list")
    public AjaxJson list(M3u8Video m3u8Video) {
        AjaxJson ajaxJson = AjaxJson.success();
        ajaxJson.put("list", m3u8ServerService.findAllList(m3u8Video));
        return ajaxJson;
    }

    @GetMapping("get")
    public AjaxJson get(M3u8Video m3u8Video) {
        AjaxJson ajaxJson = AjaxJson.success();
        ajaxJson.put("video", m3u8ServerService.get(m3u8Video));
        return ajaxJson;
    }

    @PostMapping("save")
    public AjaxJson save(M3u8Video video) {
        int sqlResult = m3u8ServerService.save(video);
        AjaxJson ajaxJson = AjaxJson.success();
        if (sqlResult < 1) {
            ajaxJson.put("msg", "添加失败");
        }
        return ajaxJson;
    }

    @PostMapping("typeCheck")
    public FileTypeCheck videoTypeCheck(String filePath) {
        File file = new File(filePath);
        return new FileTypeCheck(file);
    }
}
